package yxy.game.pm2.command.capturestrategicpoints;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

import java.util.List;

/**
 * @since 2022-08-16
 * @author wrs
 * @descrition 斩关夺隘布阵保存
 */
@MessageType(proto = Command.CaptureStrategicPointsFormatSave, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveRequest.class)
public class CMD_CaptureStrategicPointsFormatSave extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        List<FormatMessage.Format> formatList = request.getFormatListList();
        CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveResponse.newBuilder();

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }
            CaptureStrategicPointsHandle handle = new CaptureStrategicPointsHandle();

            //todo 检查队伍并保存
            Result result = handle.formatSet(playerCode, formatList,module);

            send(messageSource,response,result);
        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsFormatSaveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
